import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonFrame extends JFrame {
    private JPanel buttonPanel;
    private JButton yellowButton;
    private JButton blueButton;
    private JButton redButton;

    public ButtonFrame() {


        //创建按钮事件
        ColorAction yellowAction = new ColorAction(Color.YELLOW,buttonPanel);
        ColorAction blueAction = new ColorAction(Color.BLUE,buttonPanel);
        ColorAction redAction = new ColorAction(Color.RED,buttonPanel);
        yellowButton.addActionListener(yellowAction);
        redButton.addActionListener(redAction);
        blueButton.addActionListener(blueAction);

        //将 panel 添加到窗体 frame 对象中
        setContentPane(buttonPanel);

        setSize(500,300);
        setLayout(new FlowLayout());

        setLocationRelativeTo(null);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        //使窗体可视，就是让窗体显示出来，
        // 一般要放在最后一句，前面必须都配置完成
        this.setVisible(true);
    }

    public static void main(String[] args){
        new ButtonFrame();
    }
}
